//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YoYInflationIndex.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Indexes/Region.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/InflationIndex.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (String^ familyName, Cephei::QL::Indexes::IRegion^ region, Boolean revised, Boolean interpolated, Boolean ratio, QL::Times::FrequencyEnum frequency, Cephei::QL::Times::IPeriod^ availabilityLag, Cephei::QL::ICurrency^ currency, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>^ ts) : CInflationIndex(CYoYInflationIndex::typeid)
{
    CRegion^ _Cregion;
    CPeriod^ _CavailabilityLag;
    CCurrency^ _Ccurrency;
    CYoYInflationTermStructure^ _Cts;
    try
    {
#ifdef HANDLE
        _phYoYInflationIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName);
        _Cregion = safe_cast<CRegion^> (region);
        _Cregion->Lock();
        QuantLib::Region& _region = static_cast<QuantLib::Region&> (_Cregion->GetReference ()); 
        bool _revised = (bool)ValueHelper::Convert (revised);
        bool _interpolated = (bool)ValueHelper::Convert (interpolated);
        bool _ratio = (bool)ValueHelper::Convert (ratio);
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        _CavailabilityLag = safe_cast<CPeriod^> (availabilityLag);
        _CavailabilityLag->Lock();
        QuantLib::Period& _availabilityLag = static_cast<QuantLib::Period&> (_CavailabilityLag->GetReference ()); 
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>::IsSome::get (ts))
        {
            _Cts = safe_cast<CYoYInflationTermStructure^> (ts->Value);
            _Cts->Lock();
        }
        Handle<QuantLib::YoYInflationTermStructure>& _ts = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>::IsSome::get (ts) ? static_cast<Handle<QuantLib::YoYInflationTermStructure>&> (_Cts->GetHandle ()) : Handle<QuantLib::YoYInflationTermStructure>()); //1
        _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (new QuantLib::YoYInflationIndex ( _familyName,  _region,  _revised,  _interpolated,  _ratio,  _frequency,  _availabilityLag,  _currency,  _ts ));
        SetInflationIndex (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cregion != nullptr) _Cregion->Unlock();
        if (_CavailabilityLag != nullptr) _CavailabilityLag->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (boost::shared_ptr<QuantLib::YoYInflationIndex>& childNative, Object^ owner) : CInflationIndex(CYoYInflationIndex::typeid)
{
#ifdef HANDLE
	_phYoYInflationIndex = NULL;
#endif
	_ppYoYInflationIndex = &childNative;
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
}
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (QuantLib::YoYInflationIndex& childNative, Object^ owner) : CInflationIndex(CYoYInflationIndex::typeid)
{
#ifdef HANDLE
	_phYoYInflationIndex = NULL;
#endif
	_ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (&childNative);
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
    _YoYInflationIndexOwner = owner;
    _InflationIndexOwner = owner;
}

Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (CYoYInflationIndex^ copy) : CInflationIndex(CYoYInflationIndex::typeid)
{
#ifdef HANDLE
	_phYoYInflationIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (copy->GetShared());
        _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
    }
}
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (System::Type^ t) : CInflationIndex(CYoYInflationIndex::typeid)
{
#ifdef HANDLE
	_phYoYInflationIndex = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationIndex::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (QuantLib::Handle<QuantLib::YoYInflationIndex>& childNative, Object^ owner)  : CInflationIndex(CYoYInflationIndex::typeid)
{
	_phYoYInflationIndex = &childNative;
	_ppYoYInflationIndex = &static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>>(childNative.currentLink());
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
    _YoYInflationIndexOwner = owner;
}
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (QuantLib::Handle<QuantLib::YoYInflationIndex> childNative)  : CInflationIndex(CYoYInflationIndex::typeid)
{
	_phYoYInflationIndex = &childNative;
	_ppYoYInflationIndex = &static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>>(childNative.currentLink());
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::CYoYInflationIndex::CYoYInflationIndex (QuantLib::YoYInflationIndex childNative)  : CInflationIndex(CYoYInflationIndex::typeid)
{
#ifdef HANDLE
	_phYoYInflationIndex = NULL;
#endif
	_ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (new QuantLib::YoYInflationIndex (childNative));
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppYoYInflationIndex));
}
#endif

Cephei::QL::Indexes::CYoYInflationIndex::~CYoYInflationIndex ()
{
    if (_ppYoYInflationIndex != NULL)
    {
	    delete _ppYoYInflationIndex;
        _ppYoYInflationIndex = NULL;
    }
}
Cephei::QL::Indexes::CYoYInflationIndex::!CYoYInflationIndex ()
{
    if (_ppYoYInflationIndex != NULL)
    {
	    delete _ppYoYInflationIndex;
    }
}
QuantLib::YoYInflationIndex& Cephei::QL::Indexes::CYoYInflationIndex::GetReference ()
{
    if (_ppYoYInflationIndex == NULL) throw gcnew NativeNullException ();
	return **_ppYoYInflationIndex;
}
boost::shared_ptr<QuantLib::YoYInflationIndex>& Cephei::QL::Indexes::CYoYInflationIndex::GetShared ()
{
    if (_ppYoYInflationIndex == NULL) throw gcnew NativeNullException ();
	return *_ppYoYInflationIndex;
}
QuantLib::YoYInflationIndex* Cephei::QL::Indexes::CYoYInflationIndex::GetPointer ()
{
    if (_ppYoYInflationIndex == NULL) throw gcnew NativeNullException ();
	return &**_ppYoYInflationIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationIndex>& Cephei::QL::Indexes::CYoYInflationIndex::GetHandle ()
{
	if (_phYoYInflationIndex == NULL)
	{
		_phYoYInflationIndex = new Handle<QuantLib::YoYInflationIndex> (*_ppYoYInflationIndex);
	}
	return *_phYoYInflationIndex;
}
#endif
bool Cephei::QL::Indexes::CYoYInflationIndex::HasNative () 
{
	return (_ppYoYInflationIndex != NULL);
}

Double Cephei::QL::Indexes::CYoYInflationIndex::Fixing (DateTime fixingDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forecastTodaysFixing)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
        bool _forecastTodaysFixing = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forecastTodaysFixing) ? (bool)ValueHelper::Convert (forecastTodaysFixing->Value) : false); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppYoYInflationIndex)->fixing ( _fixingDate,  _forecastTodaysFixing );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Indexes::CYoYInflationIndex::Ratio::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppYoYInflationIndex)->ratio ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYoYInflationTermStructure^ Cephei::QL::Indexes::CYoYInflationIndex::YoyInflationTermStructure::get ()
{
    try
    {
    	Handle<QuantLib::YoYInflationTermStructure> _rv = (Handle<QuantLib::YoYInflationTermStructure>)(*_ppYoYInflationIndex)->yoyInflationTermStructure ( );   
        Cephei::QL::Termstructures::CYoYInflationTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYoYInflationTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IYoYInflationIndex^ Cephei::QL::Indexes::CYoYInflationIndex::Clone (Cephei::QL::Termstructures::IYoYInflationTermStructure^ h)
{
    CYoYInflationTermStructure^ _Ch;
    try
    {
        _Ch = safe_cast<CYoYInflationTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::YoYInflationTermStructure>& _h = static_cast<Handle<QuantLib::YoYInflationTermStructure>&> (_Ch->GetHandle ()); 
    	boost::shared_ptr<QuantLib::YoYInflationIndex> _rv = (boost::shared_ptr<QuantLib::YoYInflationIndex>)(*_ppYoYInflationIndex)->clone ( _h );   
        Cephei::QL::Indexes::CYoYInflationIndex^ _nrv = gcnew Cephei::QL::Indexes::CYoYInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::IYoYInflationIndex^ Cephei::QL::Indexes::CYoYInflationIndex_Factory::Create (String^ familyName, Cephei::QL::Indexes::IRegion^ region, Boolean revised, Boolean interpolated, Boolean ratio, QL::Times::FrequencyEnum frequency, Cephei::QL::Times::IPeriod^ availabilityLag, Cephei::QL::ICurrency^ currency, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>^ ts)
{
    return gcnew CYoYInflationIndex ( familyName,  region,  revised,  interpolated,  ratio,  frequency,  availabilityLag,  currency,  ts);
}
